//
//  VCRegister.m
//  MOSAD_HW2_1
//
//  Created by itlab on 2020/10/30.
//  Copyright © 2020 Jason. All rights reserved.
//

#import "VCRegister.h"

@interface VCRegister ()

@end

@implementation VCRegister

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"注册";
    self.navigationController.navigationBar.translucent = NO;
    //设置背景颜色
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    //设置登录界面内容
    //用户名提示标签创建
    _lbUserName = [[UILabel alloc] init];
    _lbUserName.frame = CGRectMake(20, 60, 80, 40);
    _lbUserName.text = @"用户名:";
    _lbUserName.font = [UIFont systemFontOfSize:24];
    _lbUserName.textAlignment = NSTextAlignmentLeft;
    
    //用户密码提示
    _lbPassword = [[UILabel alloc] init];
    _lbPassword.frame = CGRectMake(20, 144, 80, 40);
    _lbPassword.text = @"密码:";
    _lbPassword.font = [UIFont systemFontOfSize:24];
    _lbPassword.textAlignment = NSTextAlignmentLeft;
    
    //邮箱
    _lbEmail = [[UILabel alloc] init];
    _lbEmail.frame = CGRectMake(20, 228, 80, 40);
    _lbEmail.text = @"邮箱:";
    _lbEmail.font = [UIFont systemFontOfSize:24];
    _lbEmail.textAlignment = NSTextAlignmentLeft;

    //电话
    _lbPhoneNum = [[UILabel alloc] init];
    _lbPhoneNum.frame = CGRectMake(20, 312, 80, 40);
    _lbPhoneNum.text = @"电话:";
    _lbPhoneNum.font = [UIFont systemFontOfSize:24];
    _lbPhoneNum.textAlignment = NSTextAlignmentLeft;
    
    //用户名输入框
    _tfUserName = [[UITextField alloc] init];
    _tfUserName.frame = CGRectMake(120, 60, self.view.bounds.size.width-140, 40);
    _tfUserName.placeholder = @"请输入用户名";
    _tfUserName.borderStyle = UITextBorderStyleRoundedRect;
    
    //密码输入框
    _tfPassword = [[UITextField alloc] init];
    _tfPassword.frame = CGRectMake(120, 144, self.view.bounds.size.width-140, 40);
    _tfPassword.placeholder = @"请输入密码";
    _tfPassword.borderStyle = UITextBorderStyleRoundedRect;
    //密码是否保密
    _tfPassword.secureTextEntry = YES;
    
    //邮箱输入框
    _tfEmail = [[UITextField alloc] init];
    _tfEmail.frame = CGRectMake(120, 228, self.view.bounds.size.width-140, 40);
    _tfEmail.placeholder = @"请输入邮箱地址";
    _tfEmail.borderStyle = UITextBorderStyleRoundedRect;

    //电话输入框
    _tfPhoneNum = [[UITextField alloc] init];
    _tfPhoneNum.frame = CGRectMake(120, 312, self.view.bounds.size.width-140, 40);
    _tfPhoneNum.placeholder = @"请输入电话号码";
    _tfPhoneNum.borderStyle = UITextBorderStyleRoundedRect;

    
    _btRegister = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _btRegister.frame = CGRectMake(0, 0, 80, 80);
    _btRegister.titleLabel.font = [UIFont systemFontOfSize:24];
    _btRegister.center = CGPointMake(self.view.bounds.size.width/2, 440);
    [_btRegister setTitle:@"注册" forState:UIControlStateNormal];
    [_btRegister addTarget:self action:@selector(pressRegister) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_lbUserName];
    [self.view addSubview:_lbPassword];
    [self.view addSubview:_lbEmail];
    [self.view addSubview:_lbPhoneNum];
    [self.view addSubview:_tfUserName];
    [self.view addSubview:_tfPassword];
    [self.view addSubview:_tfEmail];
    [self.view addSubview:_tfPhoneNum];
    [self.view addSubview:_btRegister];
}



-(void) pressRegister {
    if(_tfUserName.text.length == 0 || _tfPassword.text.length == 0 || _tfEmail.text.length == 0 || _tfPhoneNum.text.length == 0) {
        UIAlertAction * OK = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
        if(_tfUserName.text.length == 0) {
            UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"警告" message:@"用户名未填写!" preferredStyle:UIAlertControllerStyleAlert];
            [alertController addAction:OK];
            [self presentViewController:alertController animated:YES completion:nil];
        }
        else if(_tfPassword.text.length == 0) {
            UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"警告" message:@"密码未填写!" preferredStyle:UIAlertControllerStyleAlert];
            [alertController addAction:OK];
            [self presentViewController:alertController animated:YES completion:nil];
        }
        else if (_tfEmail.text.length == 0) {
            UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"警告" message:@"邮箱地址未填写!" preferredStyle:UIAlertControllerStyleAlert];
            [alertController addAction:OK];
            [self presentViewController:alertController animated:YES completion:nil];
        }
        else if (_tfPhoneNum.text.length == 0) {
            UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"警告" message:@"电话号码未填写!" preferredStyle:UIAlertControllerStyleAlert];
            [alertController addAction:OK];
            [self presentViewController:alertController animated:YES completion:nil];
        }
    }
    else {
        //注册成功的弹窗，持续时间0.5s
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功！" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertController animated:YES completion:nil];
        [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerAction:) userInfo:alertController repeats:NO];
        
        
    }

    
}

-(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //回收键盘对象
    [_tfUserName resignFirstResponder];
    [_tfPassword resignFirstResponder];
    [_tfEmail resignFirstResponder];
    [_tfPhoneNum resignFirstResponder];
}

-(void)timerAction:(NSTimer *) timer {
    UIAlertController * alert = (UIAlertController *)[timer userInfo];
    [alert dismissViewControllerAnimated:YES completion:nil];
    alert = nil;
    
    //添加全局变量
    AppDelegate * myDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    //定义一个用户默认数据对象
    NSUserDefaults * ud = [NSUserDefaults standardUserDefaults];
    //存储字符串对象
    //P1:要存储的对象
    //P2:对象名字KEY值
    [ud setObject:_tfUserName.text forKey:[NSString stringWithFormat:@"USERNAME%ld", myDelegate.number]];
    [ud setObject:_tfPassword.text forKey:[NSString stringWithFormat:@"PASSWORD%ld", myDelegate.number]];
    [ud setObject:_tfEmail.text forKey:[NSString stringWithFormat:@"EMAIL%ld", myDelegate.number]];
    [ud setObject:_tfPhoneNum.text forKey:[NSString stringWithFormat:@"PHONENUM%ld", myDelegate.number]];
    myDelegate.number++;
    
    VCMy * vcMy = [[VCMy alloc] init];
    vcMy.userName = _tfUserName.text;
    vcMy.phoneNum = _tfPhoneNum.text;
    vcMy.email = _tfEmail.text;
    [self.navigationController pushViewController:vcMy animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
